package br.com.iswe.gwt.ui.client.comp;

import br.com.iswe.gwt.util.client.FlatMap;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.MouseListenerCollection;
import com.google.gwt.user.client.ui.SourcesMouseEvents;

/**
 * HyperLink que propriedades.
 * 
 * @author xzmy1p
 * 
 */
public class HyperLinkBag extends Hyperlink implements PropertiesBag, SourcesMouseEvents {

	private static final String LINK_DETALHAR = "LINK_DETALHAR";

	private MouseListenerCollection mouseListeners;

	private String targetHistoryToken;

	private Element anchorElem;

	public HyperLinkBag() {
		setElement(DOM.createDiv());
		DOM.appendChild(getElement(), anchorElem = DOM.createAnchor());
		sinkEvents(Event.ONCLICK | Event.ONMOUSEOVER | Event.ONMOUSEOUT);
		setStyleName("gwt-Hyperlink");
	}

	public HyperLinkBag(String texto) {
		this(texto, LINK_DETALHAR);
	}

	public HyperLinkBag(String texto, String historyToken) {
		this();
		setText(texto);
		setTargetHistoryToken(historyToken);
	}

	public void setTargetHistoryToken(String targetHistoryToken) {
		this.targetHistoryToken = targetHistoryToken;
		DOM.setElementProperty(anchorElem, "href", "#" + targetHistoryToken);
	}

	public void setText(String text) {
		DOM.setInnerText(anchorElem, text);
	}
	
	public void setHTML(String html) {
	    DOM.setInnerHTML(anchorElem, html);
	  }

	public void addMouseListener(MouseListener listener) {
		if (mouseListeners == null) {
			mouseListeners = new MouseListenerCollection();
		}
		mouseListeners.add(listener);
	}
	public void removeMouseListener(MouseListener listener) {
		if (mouseListeners != null) {
			mouseListeners.remove(listener);
		}
	}
	
	public String getHTML() {
	    return DOM.getInnerHTML(anchorElem);
	  }

	  /**
	   * Gets the history token referenced by this hyperlink.
	   * 
	   * @return the target history token
	   * @see #setTargetHistoryToken
	   */
	  public String getTargetHistoryToken() {
	    return targetHistoryToken;
	  }

	  public String getText() {
	    return DOM.getInnerText(anchorElem);
	  }

	  public void onBrowserEvent(Event event) {
			int eventType = DOM.eventGetType(event);
			switch (eventType) {
			case Event.ONMOUSEOVER:
			case Event.ONMOUSEOUT:
				if (mouseListeners != null) {
					mouseListeners.fireMouseEvent(this, event);
				}
				break;
			default:
				super.onBrowserEvent(event);
			}
			
		}
	
	private FlatMap bean = FlatMap.getInstance();

	public String getPropriedade(String chave) {
		return bean.getString(chave);
	}

	public void setPropriedade(String chave, String valor) {
		bean.put(chave, valor);
	}

	public void setBean(FlatMap bean) {
		this.bean.putAll( bean );
	}

	public FlatMap getBean() {
		return bean;
	}

}
